package cn.gogpay.duoim.sdk.service.v3;

import cn.gogpay.duoim.sdk.model.IMRespResult;
import cn.gogpay.duoim.sdk.model.v3.base.IMCertificate;
import cn.gogpay.duoim.sdk.model.v3.conversation.req.*;
import cn.gogpay.duoim.sdk.model.v3.conversation.resp.GetAllConversationsResp;
import cn.gogpay.duoim.sdk.model.v3.conversation.resp.GetConversationOfflinePushUserIDsResp;
import cn.gogpay.duoim.sdk.model.v3.conversation.resp.GetConversationResp;
import cn.gogpay.duoim.sdk.model.v3.conversation.resp.GetConversationsResp;
import cn.gogpay.duoim.sdk.service.IMApiFactory;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import okhttp3.Request;

public class IMConversationV3Service {

    /**
     * getAllConversations
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<GetAllConversationsResp> getAllConversations(IMCertificate token, GetAllConversationsReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/conversation/get_all_conversations");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<GetAllConversationsResp>> typeReference = new TypeReference<IMRespResult<GetAllConversationsResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * getConversation
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<GetConversationResp> getAllConversations(IMCertificate token, GetConversationReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/conversation/get_conversation");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<GetConversationResp>> typeReference = new TypeReference<IMRespResult<GetConversationResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * getConversations
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<GetConversationsResp> getConversations(IMCertificate token, GetConversationsReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/conversation/get_conversations");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<GetConversationsResp>> typeReference = new TypeReference<IMRespResult<GetConversationsResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * setConversations
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<GetConversationsResp> setConversations(IMCertificate token, SetConversationsReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/conversation/set_conversations");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<GetConversationsResp>> typeReference = new TypeReference<IMRespResult<GetConversationsResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * setConversations
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<GetConversationOfflinePushUserIDsResp> getConversationOfflinePushUserIDs(IMCertificate token, GetConversationOfflinePushUserIDsReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/conversation/get_conversation_offline_push_user_ids");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<GetConversationOfflinePushUserIDsResp>> typeReference = new TypeReference<IMRespResult<GetConversationOfflinePushUserIDsResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }
}
